package gnacos

import (
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

type ConfigService struct {
	iClient config_client.IConfigClient
}

func GenerateConfigService(namespace string, addrs []Addr, options ...OptFunc) (*ConfigService, error) {
	initConfig(namespace, addrs, options...)
	client, err := clients.NewConfigClient(vo.NacosClientParam{
		ClientConfig:  clientConfig,
		ServerConfigs: serverConfigs,
	})
	if err != nil {
		return nil, err
	}
	return &ConfigService{
		client,
	}, nil
}

func (config *ConfigService) GetStr(group, dataId string) (string, error) {
	return config.iClient.GetConfig(vo.ConfigParam{
		DataId: dataId,
		Group:  group,
	})
}

func (config *ConfigService) AddListener(param vo.ConfigParam) error {
	return config.iClient.ListenConfig(param)
}
